package com.sec.etech.employeesync.handler;

import com.sec.etech.employeesync.entity.OrgEmployeeZEthnicity;
import com.sec.etech.employeesync.mapper.OrgEmployeeZEthnicityMapper;
import com.sec.etech.employeesync.util.ConfigHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class EthnicityHandler implements ConfigHandler {

    @Resource
    private OrgEmployeeZEthnicityMapper ethnicityMapper;

    @Override
    public Long createConfig(String... params) {
        String name = params[0];

        OrgEmployeeZEthnicity ethnicity = new OrgEmployeeZEthnicity();
        ethnicity.setName(name);
        ethnicity.setActive(1);
        ethnicityMapper.insert(ethnicity);

        log.info("新增民族配置:name={}", name);
        return ethnicity.getEthnicityId();
    }

    @Override
    public List<ConfigEntry> getAllConfigs() {
        return ethnicityMapper.selectList(null).parallelStream()
                .map(item -> {
                    ConfigEntry entry = new ConfigEntry();
                    entry.setId(item.getEthnicityId());
                    entry.setParams(new String[]{item.getName()});
                    return entry;
                })
                .collect(Collectors.toList());
    }
}
